package com.work.straighttalk;

import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.bean.StraightTalkMessage;

import java.io.File;

/**
 * 播放器类
 * Created by 朱相卿 on 16/3/10.
 */
public class StraightTalkPlayer {
    private MediaPlayer mediaPlayer;
    private String msgID;
    private boolean isPlaying;
    private int length;
    private PlayerCallBack playerCallBack;

    public void setPlayerCallBack(PlayerCallBack playerCallBack) {
        this.playerCallBack = playerCallBack;
    }

    /**
     * 播放器初始化
     */
    public StraightTalkPlayer() {
        msgID = new String();
        isPlaying = false;
        mediaPlayer = new MediaPlayer();
        length = 0;
    }

    public void playVoice(StraightTalkMessage message) {
        EMMessage emMessage = message.getEmMessage();
        EMVoiceMessageBody body = (EMVoiceMessageBody) emMessage.getBody();
        String filePath = body.getLocalUrl();
        String newMsgID = emMessage.getMsgId();
        if (isPlaying) {
            if (msgID.equals(newMsgID)) {
                stopPlayVoice();
                return;
            } else {
                stopPlayVoice();
            }
        }
        if (!(new File(filePath).exists())) {
            return;
        }
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepare();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    stopPlayVoice(); // stop animation
                }

            });
            mediaPlayer.start();
            isPlaying = true;
            msgID = newMsgID;
            seekTo(message.getProgress());
            // 如果是接收的消息
        } catch (Exception e) {
            System.out.println();
        }
    }

    public void stopPlayVoice() {
        // stop play voice
        isPlaying = false;
        if (null != mediaPlayer) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            length = 0;
        }
        if (null != playerCallBack) {
            playerCallBack.onStop();
        }
    }

    /**
     * .获取 当前播放进度
     *
     * @return
     */
    public int getCurrentPosition() {
        if (isPlaying) {
            return mediaPlayer.getCurrentPosition();
        } else return 0;
    }

    public void pausePlayer() {
        isPlaying = false;
        mediaPlayer.pause();
    }

    /**
     * 控制播放器 进度
     */
    public void seekTo(int progress) {
        if (isPlaying) {
            mediaPlayer.seekTo(progress);
        }
    }

    /**
     * 获取总时长
     */
    public int getDuration() {
        if (isPlaying) {
            return mediaPlayer.getDuration();
        }
        return -1;
    }

    public boolean isPlaying() {
        return isPlaying;
    }

  public interface PlayerCallBack {
        void onStop();
    }
}
